<?php

class ImageController extends MyController
{
	function init() {parent::init(); }
	
	function actionIndex()
	{	
		$this->redirect(array($this->controller_id.'/view'));
	}	
	
	function actionView($icategory_id = 0, $page = 1)
	{
		$pages = new CPagination();
		$pages->pageSize = $this->website->beperpage;
		
		$rows = NULL;
		
		if ($icategory_id > 0)
		{
			$rows = image::model()->findAll(
				array(
					"condition"=>"icategory_id = " . $icategory_id,
					"order" => "`order` ASC, create_time DESC",
					"offset" => ($page-1)*$this->website->beperpage,
					"limit" => $this->website->beperpage
				)
			);
			
			$pages->itemCount = image::model()->count(array(
				"condition"=>"icategory_id = " . $icategory_id
			));
			
		}
		else
		{
			$rows = image::model()->findAll(array(
				"order" => "`order` ASC, create_time DESC, id ASC",
				"offset" => ($page-1) * $this->website->beperpage,
				"limit" => $this->website->beperpage
			));
			
			$pages->itemCount = image::model()->count();
			
		}
		
		$this->data['rows']= $rows;	
		$this->data['icategory_id']= $icategory_id;	
		$this->data['pages']= $pages;
		$this->data["page"] = $page;
		
		$this->render('view', $this->data);
	}
	
	function actionAdd($icategory_id = 0, $page = 1)
	{
		$row = new image;
		$row->id = 0;
		$row->create_time = MyHelper::get_datetime();
		$this->data["row"] = $row;
		
		$this->data['icategory_id']= $icategory_id;	
		$this->data["page"] = $page;
		$this->render("form_upload", $this->data);
	}
	
	function actionEdit($image_id = 0, $icategory_id = 0, $page = 1)
	{
		$row = image::model()->findByPk($image_id);

		$this->data["row"] = $row;		
		$this->data['icategory_id']= $icategory_id;	
		$this->data["page"] = $page;
		$this->render("form_edit", $this->data);
	}
	
	function actionSave($image_id = 0, $icategory_id = 0, $page = 1)
	{
		$image = NULL;
		if ($image_id == 0)
		{
			$row = new image;
			$row->setAttributes($_POST, false);
			$row->user_id = $this->user->id;
			$row->enable = 1;
			
			$row->save();
			
		}else{
			$row = image::model()->findByPk($image_id);
			$row->setAttributes($_POST, false);
			$row->save();
		}
		
		$this->redirect(array($this->controller_id.'/view', 
			"icategory_id"=>$icategory_id, "page" => $page));
	}
	
	function actionEnable($image_id = 0, $icategory_id = 0, $page = 1)
	{
		$row = image::model()->findByPk($image_id);
		$row->enable = 1 - $row->enable;
		$row->save();
		
		if ($row->trash == 0)
			$this->redirect(array($this->controller_id.'/view', 
				"icategory_id"=>$icategory_id, "page" => $page));
		else
			$this->redirect(array($this->controller_id.'/view_trash', 
				"icategory_id"=>$icategory_id, "page" => $page));
	}
	
	function actionDelete($image_id = 0, $icategory_id = 0, $page = 1)
	{	
		// move image into the trash
		$row = image::model()->findByPk($image_id);
		$row->delete();
		
		$rows = NULL;
		
		if ($icategory_id > 0)
		{
			$rows = image::model()->findAll(array(
				"condition"=>"icategory_id = " . $icategory_id,
				"offset" => ($page-1)*$this->website->beperpage,
				"limit" => $this->website->beperpage
			));
				
		}
		else
		{
			$rows = image::model()->findAll(array(
				"offset" => ($page-1)*$this->website->beperpage,
				"limit" => $this->website->beperpage
			));	
		}
		
		if (count($rows) == 0)
		{
			if ($page > 1)
				$page --;
		}
		
		$this->redirect(array($this->controller_id.'/view', 
			"icategory_id"=>$icategory_id, "page" => $page));	
	}

	function actionDelete_multi($icategory_id = 0, $page = 1)
	{
		//var_dump(Yii::app()->getRequest()->getPost("cib"));
		
		$id_array = Yii::app()->getRequest()->getPost("cib");
		if (!is_null($id_array))
		{
			foreach($id_array as $id=>$value)
			{
				$row = image::model()->findByPk($id);
				$row->delete();
			}
		}
		
		$rows = NULL;
		if ($icategory_id > 0)
		{
			$rows = image::model()->findAll(array(
				"condition"=>"icategory_id = " . $icategory_id,
				"offset" => ($page-1)*$this->website->beperpage,
				"limit" => $this->website->beperpage,
			));
				
		}
		else
		{
			$rows = image::model()->findAll(array(
				"offset" => ($page-1)*$this->website->beperpage,
				"limit"	 => $this->website->beperpage,
			));	
		}
		
		if (count($rows) == 0)
		{
			if ($page > 1)
				$page --;
		}
		
		$this->redirect(array($this->controller_id.'/view', 
			"icategory_id"=>$icategory_id, "page"=>$page));
	}
	
}